package cn.com.headfree.rzgl.dao.impl;

import cn.com.headfree.entity.IscUser;
import cn.com.headfree.entity.TEmpBaseInfo;
import cn.com.headfree.entity.TOrgDept;
import cn.com.headfree.rzgl.dao.IRzglDao;
import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;


@Repository("rzglDao")
public class RzglDaoImpl extends BaseDaoImpl<IscUser> implements IRzglDao {

    /**
     * 查询人员信息
     * @param page
     * @param iscUser
     */
//    @Override
//    public void searchUserList(Page<IscUser> page, IscUser iscUser){
//        ArrayList<Object> arrayList = new ArrayList<>();
//        StringBuilder sqlbuilder = new StringBuilder(" SELECT IU.id AS ID, IU.name AS NAME, IU.fax AS FAX,TOD.id AS dept_id, TOD.name AS ORG_ANISATION_NAME,TDER.org_id as BASEORG_ID, TEBI.mobile_phone AS TEL, TEBI.cur_post AS TITLE ");
//        sqlbuilder.append(" FROM isc_user IU LEFT JOIN t_dept_emp_rela TDER ON IU.fax = TDER.erp_no LEFT JOIN t_org_dept TOD ON TOD.id = TDER.dept_id LEFT JOIN t_emp_base_info TEBI ON TEBI.erp_no = IU.fax ");
//        sqlbuilder.append(" WHERE 1=1 AND TEBI.del_sign = 0 AND TEBI.is_valid = 0 ");
//        if (null != iscUser) {
//            //erp员工号
//            if (!StringUtil.isNullOrEmpty(iscUser.getFax())) {
//                sqlbuilder.append(" AND IU.fax LIKE concat('%',?,'%') ESCAPE '/'");
//                String replaceStr = iscUser.getFax().replaceAll("%", "/%").replaceAll("_", "/_");
//                arrayList.add(replaceStr);
//            }
//            //人员名称
//            if (!StringUtil.isNullOrEmpty(iscUser.getName())) {
//                sqlbuilder.append(" AND IU.name LIKE concat('%',?,'%') ESCAPE '/'");
//                String replaceStr = iscUser.getName().replaceAll("%", "/%").replaceAll("_", "/_");
//                arrayList.add(replaceStr);
//            }
//            if (!StringUtil.isNullOrEmpty(iscUser.getBaseorgId())) {
//                sqlbuilder.append(" AND TOD.path LIKE concat('%',?,'%') ");
//                arrayList.add(iscUser.getBaseorgId());
//            }
//        }
//        sqlbuilder.append(" ORDER BY TEBI.sort ASC");
//        this.findPageBySql(IscUser.class, sqlbuilder.toString(), page, arrayList.toArray());
//    }
    @Override
    public void searchUserList(Page<IscUser> page, IscUser iscUser){
        ArrayList<Object> arrayList = new ArrayList<>();
        StringBuilder sqlbuilder = new StringBuilder(" SELECT TEBI.id as ID,TEBI.NAME as NAME,TEBI.erp_no as FAX,TOD.id as dept_id,TOD.NAME as ORG_ANISATION_NAME,TDER.org_id as BASEORG_ID,TEBI.mobile_phone as TEL,TEBI.cur_post as TITLE ");
        sqlbuilder.append(" FROM t_emp_base_info TEBI LEFT JOIN t_dept_emp_rela TDER ON TEBI.erp_no = TDER.erp_no LEFT JOIN t_org_dept TOD ON TOD.id = TDER.dept_id ");
        sqlbuilder.append(" WHERE 1=1 AND TEBI.del_sign = 0 AND TEBI.is_valid = 0 ");
        if (null != iscUser) {
            //erp员工号
            if (!StringUtil.isNullOrEmpty(iscUser.getFax())) {
                sqlbuilder.append(" AND TEBI.erp_no LIKE concat('%',?,'%') ESCAPE '/'");
                String replaceStr = iscUser.getFax().replaceAll("%", "/%").replaceAll("_", "/_");
                arrayList.add(replaceStr);
            }
            //人员名称
            if (!StringUtil.isNullOrEmpty(iscUser.getName())) {
                sqlbuilder.append(" AND TEBI.name LIKE concat('%',?,'%') ESCAPE '/'");
                String replaceStr = iscUser.getName().replaceAll("%", "/%").replaceAll("_", "/_");
                arrayList.add(replaceStr);
            }
            if (!StringUtil.isNullOrEmpty(iscUser.getBaseorgId())) {
                sqlbuilder.append(" AND TOD.path LIKE concat('%',?,'%') ");
                arrayList.add(iscUser.getBaseorgId());
            }
        }
        sqlbuilder.append(" ORDER BY TEBI.sort ASC");
        this.findPageBySql(IscUser.class, sqlbuilder.toString(), page, arrayList.toArray());
    }

    /**
     * 表示第一次进入查询部门树
     * @param orgDept
     * @return
     */
    @Override
    public List<TOrgDept> searchTopDept(TOrgDept orgDept){
        ArrayList<Object> arrayList = new ArrayList<>();
        StringBuilder sqlbuilder = new StringBuilder(" SELECT TOD.id AS ID, TOD.name AS NAME, TOD.p_id AS P_ID, TOD.path AS PATH, TOD.level AS LEVEL ");
        sqlbuilder.append(" FROM t_org_dept TOD WHERE 1 = 1 AND TOD.id = ?  ORDER BY TOD.sort ASC ");
        arrayList.add(orgDept.getId());
        return this.findAllBySQL(TOrgDept.class, sqlbuilder.toString(), null, false, arrayList.toArray());
    }

    /**
     * 查询单位树信息
     * @param orgDept
     * @return
     */
    @Override
    public List<TOrgDept> searchDeptList(TOrgDept orgDept){
        ArrayList<Object> arrayList = new ArrayList<>();
        StringBuilder sqlbuilder = new StringBuilder(" SELECT TOD.id AS ID, TOD.name AS NAME, TOD.p_id AS P_ID, TOD.path AS PATH, TOD.level AS LEVEL ");
        sqlbuilder.append(" FROM t_org_dept TOD WHERE 1 = 1 AND TOD.p_id = ?  ORDER BY TOD.sort ASC ");
        arrayList.add(orgDept.getId());
        return this.findAllBySQL(TOrgDept.class, sqlbuilder.toString(), null, false, arrayList.toArray());
    }

}
